<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/2 0002
 * Time: 11:44
 */
//创建server对象，监听127.0.0.1:9501端口
$serv = new swoole_server("127.0.0.1", 9501);

//设置异步任务的工作进程数量
$serv->set(array('task_worker_num'=>4));

//监听链接进入事件
$serv->on('connect', function($serv, $fd){
    echo "client: connect.\n";
});

//监听数据接收事件
$serv->on('receive', function($serv, $fd, $from_id, $data){
    //异步投递
    $task_id = $serv->task($data);
    echo "Dispath AsyncTask: id=$task_id";
});

//处理异步任务
$serv->on('task', function($serv, $task_id, $from_id, $data){
    echo "New AsyncTask[id=$task_id]".PHP_EOL;
    //返回任务执行的结果
    $serv->finish("$data -> ok");
});

//处理异步任务的结果
$serv->on('finish', function($serv, $task_id, $data){
    echo "AsyncTask[$task_id] Finish: $data".PHP_EOL;
});

//监听链接关闭事件
$serv->on('close', function($serv, $fd){
    echo "Client: close.\n";
});

//启动服务
$serv->start();













